package com.leetcode;

import java.util.HashMap;

/**
 * Created by Z-hong on 2022/9/27.
 */

public class LeetCode面试0102 {
    public boolean CheckPermutation(String s1, String s2) {
        char[] ss1 = s1.toCharArray();
        char[] ss2 = s2.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < ss1.length; i++) {
            if (map.containsKey(ss1[i])){
                map.put(ss1[i],map.get(ss1[i])+1);
            }else {
                map.put(ss1[i],1);
            }
        }
        for (int i = 0; i < ss2.length; i++) {
            if (!map.containsKey(ss2[i])){
                return false;
            }else {
                if(map.get(ss2[i])==0){
                    return false;
                }else {
                    map.put(ss2[i],map.get(ss2[i])-1);
                }
            }
        }
        return true;
    }

    //力扣最佳答案
    public boolean otherA(String s1, String s2){
        int n = s1.length(), m = s2.length();
        if(n!=m) return false;
        int[] arr = new int[26];
        for(int i = 0; i < n; i++){
            arr[s1.charAt(i)-'a']++;
            arr[s2.charAt(i)-'a']--;
        }
        for(int i : arr){
            if(i!=0) return false;
        }
        return true;
    }
}
